<template>
  <div class="app-container flex-col minH">
    <div class="topline w100p bgwhite ptb20 radius10 pl10 pr20 justify-between">
      <el-input v-model="username" style="width: 280px" placeholder="搜尋用戶昵稱..." :prefix-icon="Search" class="search" />
      <div>
        <el-button class="btn2" @click="getAdminList()">查詢</el-button>
        <el-button class="btn1" @click="reset()">重置</el-button>
      </div>
    </div>
    <div class="mt15 radius10 hidden pb15 flex1 bgwhite  flex-col w100p">
      <div class="flex1 w100p ">
        <el-table :data="tableData" style="width: 100%" cell-class-name="tbcell" header-cell-class-name="hdcell">
          <el-table-column prop="id" label="ID" width="60" />
          <!-- <el-table-column prop="roles.name" label="上級訊息" width="150" /> -->
          <el-table-column prop="username" label="昵稱" width="150" />
          <el-table-column prop="roles" label="角色" width="200">
            <template #default="scope">
              <el-tag disable-transitions>{{ scope.row.roles[0].name }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="date" label="權限" width="150">
            <template #default="scope">

              <div class=" pointer" @click="edit(scope.row, 'check')">
                <el-icon>
                  <View />
                </el-icon>
                查看
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="created_at" label="創建時間" min-width="200" />
          <el-table-column prop="updated_at" label="更新時間" sortable min-width="200" />
          <el-table-column prop="" label="操作" width="80" align="right" fixed="right">
            <template #default="scope">
              <div class=" pointer" @click="edit(scope.row, 'edit')">編輯</div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class=" mrauto plr20 mt15">
        <el-button class="btn2" @click="toAdd()">新增</el-button>
      </div>
    </div>

  </div>
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router';
import { adminList } from '@/api/systemAdmin.js'

onMounted(() => {
  getAdminList()
})
const getAdminList = () => {
  adminList({
    username: username.value,
    page: 1,
    per_page: 1000
  }).then((res) => {
    console.log(res)
    tableData.value = res.data.data
  })
}

const username = ref('')
const tableData = ref([])
const reset = () => {
  username.value = ''
  getAdminList()
}
const tableRowClassName = () => {
  return 'tbrow'
}
const currentPage3 = ref(5)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
}
const router = useRouter()
const toAdd = () => {
  console.log(router)
  router.push({ path: 'add' })
}
const edit = (row, type) => {
  localStorage.setItem('roles', JSON.stringify(row.roles))
  router.push({ path: 'edit', query: { type: type, ...row } })
}
</script>

<style lang="scss" scoped>
.search {
  overflow: hidden;
  border-radius: 10px !important;
}
</style>
<style></style>
